<?php
namespace Org;
//header('Content-type:text/json');
require("simple_html_dom.php");

class getlist{
// $px [t]人气 [latest] 最新 [sell]销量 [price]价格
function ajaxCat($cid="0",$page="1",$px="t"){
    $url='http://'.$_SERVER['HTTP_HOST'];
    //echo dirname($url);
    $url = $url.__ROOT__."/api.php?r=index/ajaxCat&cid=".$cid."&page=".$page."&px=".$px;
    //echo $url;
    $f=curl($url);
    $b=parser($f->data->content);
    //==var_dump($f);
    return $b;
    //echo($f->data->content);
}

function ajaxNine($page="0"){
 $url='http://'.$_SERVER['HTTP_HOST'];
//echo dirname($url);

$url = $url.__ROOT__."/api.php?r=index/ajaxNine&page=".$page;
//echo $url;
$f=curl($url);
$b=parser($f->data->content);
//var_dump($f);
return $b;

//echo($f->data->content);
}

function ajaxR($page="0"){
 $url='http://'.$_SERVER['HTTP_HOST'];
//echo dirname($url);

$url = $url.__ROOT__."/api.php?r=index/ajaxR&page=".$page;

$f=curl($url);
$b=parser($f->data->content);
//var_dump($f);
return $b;

//echo($f->data->content);
}
// $px [t]人气 [latest] 最新 [sell]销量 [price]价格
function ajaxSearch($kw="",$page="0",$px="t"){
 $url='http://'.$_SERVER['HTTP_HOST'];
//echo dirname($url);

$url = $url.__ROOT__."/api.php?r=index/ajaxSearch&kw=".$kw."&page=".$page."&px=".$px;

$f=curl($url);
$b=parser($f->data->content);
//var_dump($f);
return $b;

//echo($f->data->content);
}

}












function curl($url){
    $url = $url;
    $headers = array("User-Agent"=>"Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.91 Mobile Safari/537.36");
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $output = curl_exec($ch);
    curl_close($ch);
    //打印获得的数据
    return json_decode($output);
}

function parser($text){
// 新建一个Dom实例
$html = new simple_html_dom();

// 从url中加载
//$html->load_file('http://www.jb51.net');

// 从字符串中加载
$html->load($text);

//从文件中加载
//$html->load_file('path/file/test.html');


$count=count($html->find('div[class=goods-item]'));
for ($x=0; $x<$count; $x++) {
    $main[$x]->title=$html->find('div[class=goods-item]',$x)->find("div[class=text]",0)->innertext;
    $main[$x]->img=$html->find('div[class=goods-item]',$x)->find("img[class=lazy]",0)->attr["data-original"];
    $main[$x]->qprice=$html->find('div[class=goods-item]',$x)->find("span[class=coupon-wrapper  theme-bg-color-1]",0)->find("b",0)->innertext;
    $main[$x]->price=$html->find('div[class=goods-item]',$x)->find("span[class=price]",0)->innertext;
    $main[$x]->soldnum=$html->find('div[class=goods-item]',$x)->find("span[class=sold-num]",0)->innertext;
    $str=$html->find('div[class=goods-item]',$x)->find("a[class=img]",0)->href;
    $preg="/\d+/";
    preg_match($preg,$str,$str1);
    $main[$x]->id=$str1[0];
    
    
}
$json->data=$main;
$json=json_encode($json);
return $json;
}


    



?> 